<?php
/**
 * Created by PhpStorm.
 * User: Celebration-Grey
 * Date: 2017/4/16
 * Time: 14:04
 */

namespace App\Http\Classes;

class Common
{
    public static function getRand(){
        $length = 6;
        $min = pow(10 , ($length - 1));
        $max = pow(10, $length) - 1;
        return mt_rand($min,$max);
    }

    public static function response($code = 1, $message = '', $data = null, $error = null){
        $response = [];
        $response['code'] = $code;
        $response['message'] = $message;
        $response['data'] = $data;
        $response['error'] = $error;
        return response()->json($response);
    }

    public static function isEmail($email){
        $pattern = "/^[_a-zA-Z0-9\-\.]+@([\-_a-zA-Z0-9]+\.)+[a-zA-Z0-9]{2,3}$/";
        if(preg_match($pattern,$email)){
            return true;
        }
        else{
            return false;
        }
    }

    public static function isPhone( $phone )
    {
        $pattern = '/^(((13[0-9]{1})|(15[0-35-9]{1})|(17[0-9]{1})|(18[0-9]{1}))+\d{8})$/';
        if(preg_match($pattern,$phone)){
            return true;
        }
        else{
            return false;
        }
    }

    public static function isEmailExists( $email )
    {
        $user = new User();
        return $user->isEmailExists($email);
    }

    public static function isPhoneExists( $phone )
    {
        $user = new User();
        return $user->isPhoneExists($phone);
    }

    public static function getUserIdByEmail( $email )
    {
        $user = new User();
        return $user->getIdByEmail( $email );
    }

    public static function isPassword( $password )
    {
        $pattern = '/^[0-9a-zA-Z_#]{6,16}$/';
        if(preg_match($pattern,$password)){
            return true;
        }
        else{
            return false;
        }
    }


}